package com.teamcenter.rac.massupdate.converters;

import org.eclipse.core.databinding.conversion.IConverter;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.views.properties.IPropertySource;
@SuppressWarnings({"unused"})
public class ImageDescriptor2SizedImageConverter implements IConverter {
	private static final double MAX_ENLARGE_RATIO = 4.0D;
	private boolean m_grabHorizontal = true;
	private boolean m_grabVertical = true;
	private int m_horzAlignment = 16777216;
	private int m_maxheight;
	private int m_maxwidth;
	private boolean m_option_CenterInFixedArea = true;
	private IPropertySource m_propertySource;
	private String m_propName;
	private int m_vertAlignment = 16777216;

	public ImageDescriptor2SizedImageConverter() {
		setMaxheight(75);
		setMaxwidth(100);
	}

	public ImageDescriptor2SizedImageConverter(int paramInt1, int paramInt2) {
		setMaxheight(paramInt1);
		setMaxwidth(paramInt2);
	}

	public ImageDescriptor2SizedImageConverter(IPropertySource paramIPropertySource, String paramString) {
		this.m_propertySource = paramIPropertySource;
		this.m_propName = paramString;
	}

	public ImageDescriptor2SizedImageConverter(Point paramPoint) {
		setImageSize(paramPoint);
	}

	public Object convert(Object paramObject) {
		if ((paramObject instanceof ImageDescriptor)) {
			if (this.m_propertySource != null) {
				Point localObject = (Point) this.m_propertySource.getPropertyValue(this.m_propName);
				if (localObject != null) {
					setMaxwidth(((Point) localObject).x);
					setMaxheight(((Point) localObject).y);
				}
			}
			Object localObject = (ImageDescriptor) paramObject;
			ImageData localImageData1 = ((ImageDescriptor) localObject).getImageData();
			boolean bool = (localImageData1.height > 40) && (localImageData1.width > 40);
			ImageData localImageData2 = getResizedImageData(localImageData1, bool, true);
			ImageDescriptor localImageDescriptor = ImageDescriptor.createFromImageData(localImageData2);
			return localImageDescriptor.createImage(Display.getDefault());
		}
		return null;
	}

	public Object getFromType() {
		return ImageDescriptor.class;
	}

	public int getMaxheight() {
		return this.m_maxheight;
	}

	public int getMaxwidth() {
		return this.m_maxwidth;
	}

	public Object getToType() {
		return Image.class;
	}

	public void setImageSize(Point paramPoint) {
		setMaxheight(paramPoint.y);
		setMaxwidth(paramPoint.x);
	}

	public void setMaxheight(int paramInt) {
		this.m_maxheight = paramInt;
	}

	public void setMaxwidth(int paramInt) {
		this.m_maxwidth = paramInt;
	}

	private ImageData getResizedImageData(ImageData paramImageData, boolean paramBoolean1, boolean paramBoolean2) {
		if ((this.m_maxwidth <= 0) || (this.m_maxheight <= 0) || (paramImageData.width <= 0)
				|| (paramImageData.height <= 0)) {
			return null;
		}
		double d1 = Math.min(this.m_maxwidth, this.m_maxheight);
		double d2 = Math.min(paramImageData.width, paramImageData.height);
		int i;
		int j;
		double d3;
		if (d2 < d1) {
			if ((this.m_option_CenterInFixedArea) && (!paramBoolean1)) {
				i = this.m_grabHorizontal ? this.m_maxwidth : paramImageData.width;
				j = this.m_grabVertical ? this.m_maxheight : paramImageData.height;
				ImageData localImageData = new ImageData(i, j, paramImageData.depth, paramImageData.palette);
				int k = 0;
				int m = 0;
				switch (this.m_vertAlignment) {
				case 16384:
					break;
				case 131072:
					k = i - paramImageData.width;
					break;
				case 16777216:
				default:
					k = (i - paramImageData.width) / 2;
				}
				switch (this.m_horzAlignment) {
				case 128:
					break;
				case 1024:
					m = j - paramImageData.height;
					break;
				case 16777216:
				default:
					m = (j - paramImageData.height) / 2;
				}
				for (int n = 0; n < paramImageData.width; n++) {
					for (int i1 = 0; i1 < paramImageData.height; i1++) {
						int i2 = n + k;
						int i3 = i1 + m;
						int i4 = paramImageData.getPixel(n, i1);
						int i5 = paramImageData.getAlpha(n, i1);
						localImageData.setPixel(i2, i3, i4);
						localImageData.setAlpha(i2, i3, i5);
					}
				}
				return localImageData;
			}
			if (paramBoolean1) {
				d3 = d1 / d2;
				if (d3 > 4.0D) {
					d3 = 4.0D;
				}
			} else {
				d3 = 1.0D;
			}
		} else {
			d3 = d1 / d2;
		}
		if (Math.abs(d3 - 1.0D) > 0.001D) {
			i = (int) (paramImageData.width * d3);
			j = (int) (paramImageData.height * d3);
			return paramImageData.scaledTo(i, j);
		}
		return paramImageData;
	}
}
